#include<stdio.h>
#include<sqlite3.h> 
#include<string.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
    int id;
    int age;

    char name[20];

    char sql[1024]={0};
    char*errmsg;

    if(argc!=2)
    {
        printf("please input db name\n");
        return 1;
    }
    sqlite3 * db;
    int ret=sqlite3_open(argv[1],&db);

    if(ret!=SQLITE_OK)
    {
        printf("sqlite3_open error%s",sqlite3_errmsg(db));
        return 1;
    }
    printf("sqlite open db successful!\n");
    strcpy(sql,"create table IF NOT EXISTS student(id integer primary key,name text,age integer);");
    ret= sqlite3_exec(db,sql,nullptr,nullptr,&errmsg);
    if(ret!=SQLITE_OK)
    {
        printf("exec error:%s\n",sqlite3_errmsg(db));
    }
    int rc,i,j;
    int ncolumn;
    sqlite3_stmt *stmt;
    rc=sqlite3_prepare(db,"select *from student",-1,&stmt,0);
    if(rc)
    {
        printf("query fail\n");
    }
    else{
        printf("query success\n");
        rc=sqlite3_step(stmt);//获取一行
        ncolumn=sqlite3_column_count(stmt);//获取列数
        while (rc==SQLITE_ROW)
        {
            //获取每列数据
            for(i=0;i<ncolumn;i++)
            {
                printf("%s|",sqlite3_column_text(stmt,i));
            }
            printf("\n");
            rc=sqlite3_step(stmt);
        }
        
    }
    sqlite3_finalize(stmt);
   
    sqlite3_close(db);
    return 0;
}